set more off

** matching geocoded address to SA1s. 


*** coordinates of SA1s - just do once
mif2dta using OrigData/VotingData/SA1_2011_AUST, attributes(OrigData/VotingData/SA1_attr) coordinates(OrigData/VotingData/SA1_coord) genid(id) gencentroids(SA1) type(polygon) replace
    
* first match to SA3 then match within SA3 to SA1 


*** coordinates of SA3s - just do once
mif2dta using OrigData/VotingData/SA3_2011_AUST, attributes(OrigData/VotingData/SA3_attr) coordinates(OrigData/VotingData/SA3_coord) genid(id) gencentroids(SA3) type(polygon) replace




use OrigData/VotingData/SA3_attr.dta, clear

keep if state_name_2011 =="Victoria"

rename sa3_code_2011 SA3

keep SA3 xSA3 ySA3 sa3_name_2011

save OrigData/VotingData/SA3_geocodes.dta, replace



use OrigData/VotingData/SA1_attr.dta, clear

keep if state_name_2011 =="Victoria"

keep sa1_7digitcode_2011 x y sa1_maincode_2011 sa3_code_2011

rename sa1_7digitcode SA1

rename sa3_code_2011 SA3 

save OrigData/VotingData/SA1_geocodes.dta, replace






clear

* geocodes using street name and suburb:
insheet using OrigData/spatial/Missing_SA1_geocodes.csv, comma

rename v1 streetname 
rename v2 suburb
rename v3 pcode
rename v4 latitude_geo
rename v5 longitude_geo


keep if latitude_geo <.


local p = _N

append using OrigData/VotingData/SA3_geocodes.dta



gen closest_site = .
gen distance_closest = .

gen next_site = .
gen next_distance = .

	
local s=_N
local z = `p' +1
	
forval x = 1(1)`p' {
	forval y = `z'(1)`s' {	
		local lat_y =  ySA3[`y']
		local lon_y = xSA3[`y']
		local lat_x =  latitude[`x']
		local lon_x = longitude[`x']
		geodist `lat_x'  `lon_x' `lat_y' `lon_y'
		local d = r(distance)
		replace next_site = closest_site if `d'<distance_closest[`x'] & _n==[`x']
		replace next_distance = distance_closest if closest_site[`x'] == `y' & _n==[`x']
		replace closest_site = `y' if `d'<distance_closest[`x'] & _n==[`x']
		replace distance_closest = `d' if closest_site[`x'] == `y' & _n==[`x'] 
		}	
	}
	
	
	replace SA3 = SA3[closest_site] if SA3==.

keep if closest_site<. 

keep streetname suburb lat longitude SA3 pcode

* within SA3 match to SA1
joinby SA3 using OrigData/VotingData/SA1_geocodes.dta
	
geodist ySA1 xSA1 lat longi, gen(dist) 

sort SA1 dist
collapse (first) SA1 xSA1 ySA1 dist, by(streetname suburb pcode)

merge 1:m streetname suburb pcode using Data/missingSA1_accounts.dta

drop _merge
drop if account_number==.
save Data/missing_SA1_matched.dta, replace

	
